<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>标准任务管理</title>
    <script type="text/javascript">
      var newStandardMaintenanceWin;
      var editStandardMaintenanceWin;
      var standardMaintenanceId;
      var smId;

      var templateId = <%=params[:id]%>
      Ext.onReady(function(){
        //任务管理---------------------------------------------------------
        //生成有关任务的Grid
        var standardMaintenanceDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize
          },
          proxy: new Ext.data.HttpProxy({
            url: '/standardmaintenance/getStandardMaintenances?templateId='+templateId
          }),
          autoLoad:true,
          reader:new Ext.data.JsonReader({
            totalProperty: 'total',
            root:'standardMaintenance'
          },[
            {name:'id'},
            {name:'work',mapping:'work.name'},
            {name:'team',mapping:'team.name'},
            {name:'begin_minute'},
            {name:'during_minute'},
            {
              name:'flag',
              mapping: 'flag',
              convert:function(v){
                if(v==0){
                  return '<font color=gray>无限制</font>'
                }else if(v==1){
                  return '<font color=green>有静有动</font>'
                }else{
                  return '<font color=red>有静无动</font>'
                }
              }
            },
            {name:'dynamic_during_minute'}
          ]
        )
        });

        var standardMaintenanceSM = new Ext.grid.CheckboxSelectionModel();
        var standardMaintenanceCM = new Ext.grid.ColumnModel([
          new Ext.grid.RowNumberer({width: 30}),
          standardMaintenanceSM,
          {header:'<center>作业类型</center>',sortable:true,dataIndex:'flag',width:80},
          {header:'任务',sortable:true,dataIndex:'work',width:60},
          {header:'组队',sortable:true,dataIndex:'team',width:170},
          {header:'<center>静态工作<br/>开始间隔分钟数</center>',sortable:true,dataIndex:'begin_minute',width:130},
          {header:'<center>静态工作<br/>持续分钟数</center>',sortable:true,dataIndex:'during_minute',width:120},
          {header:'<center>动态工作<br/>持续分钟数</center>',sortable:true,dataIndex:'dynamic_during_minute',width:120}
        ]);

        var standardMaintenanceGrid =  new Ext.grid.GridPanel({
          ds: standardMaintenanceDS,
          cm: standardMaintenanceCM,
          sm: standardMaintenanceSM,
          width: 870,
          height: 385,
          iconCls: 'icon-grid',

          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: standardMaintenanceDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          }),
          tbar: ['-',{
              text: '添加养护任务',
              width:50,
              iconCls: 'addItem',
              handler : function(){
                newStandardMaintenanceWin = new Ext.Window({
                  layout: 'fit',
                  width:517,
                  height:360,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  //modal: true,
                  title: '添加养护任务',
                  autoLoad: {
                    url: "/standardmaintenance/newStandardMaintenance?templateId="+templateId,
                    scripts: true
                  }
                });
                newStandardMaintenanceWin.on("destroy",function(){
                  standardMaintenanceDS.reload();
                });
                newStandardMaintenanceWin.resizable = false;
                newStandardMaintenanceWin.show();
              }
            },{
              text: '编辑养护任务',
              width:50,
              iconCls: 'editItem',
              handler : function(){
                var records = standardMaintenanceGrid.getSelectionModel().getSelections();
                if(records.length==0){
                  Ext.MessageBox.alert('提示信息', '请先选中一条记录后，再编辑。');
                }else{
                  //把表单添加到窗口中
                  standardMaintenanceId = records[0].get('id');
                  editStandardMaintenanceWin = new Ext.Window({
                    title: '编辑养护任务',
                    layout:'fit',
                    width:517,
                    height:360,
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    autoLoad: {
                      url: "/standardmaintenance/editStandardMaintenance?id=" + standardMaintenanceId,
                      scripts: true
                    }
                  });
                  editStandardMaintenanceWin.on("destroy",function(){
                    standardMaintenanceDS.reload();
                  });
                  editStandardMaintenanceWin.resizable = false;
                  editStandardMaintenanceWin.show();
                }

              }
            },
            {
              text:'删除养护任务',
              width:50,
              iconCls: 'remove',
              handler:function(){
                if(standardMaintenanceGrid.getSelectionModel().hasSelection()){
                  var rs=standardMaintenanceGrid.getSelectionModel().getSelections();
                  var ids=[];
                  var standardMaintenanceName='';
                  for(var i=0;i<rs.length;i++){
                    var data=rs[i].data;
                    ids.push(data.id);
                    standardMaintenanceName+=data.work+'<br>';
                  }
                  var keys=Ext.util.JSON.encode(ids);
                  Ext.MessageBox.confirm('警告','确定删除以下养护任务？<br><font color="red">' + standardMaintenanceName + '</font>',function (button){
                    if(button=='yes'){
                      Ext.Ajax.request({
                        url:'/standardmaintenance/delete',
                        method:'post',
                        waitMsg: '正在删除养护任务...',
                        success:function(response,opts){
                          var data = Ext.util.JSON.decode(response.responseText);
                          if(data.success&&data.info=='success') {
                            standardMaintenanceDS.reload();
                            Ext.Msg.alert('提示信息', '已成功删除养护任务。');
                          } else {
                            Ext.Msg.alert('提示信息', data.info);
                          }
                        },
                        params: {"id":keys}
                      });
                    }
                  });
                }else{
                  Ext.Msg.alert('提示信息','请选择要删除的养护任务');
                }
              }
            }]
        });

        function changeType(type){
          Ext.Ajax.request({
            url:'/standardmaintenance/assign?type='+type,
            method:'post',
            waitMsg: '正在修改养护任务作业类型...',
            success:function(response,opts){
              var data = Ext.util.JSON.decode(response.responseText);
              if(data.success&&data.info=='success') {
                standardMaintenanceDS.reload();
              } else {
                Ext.Msg.alert('提示信息', data.info);
              }
            },
            params: {"id": smId}
          });
        }

        standardMaintenanceGrid.on('rowcontextmenu', function(grid,rowindex,e){
          smId = standardMaintenanceDS.getAt(rowindex).get('id')
          e.preventDefault();
          rightClick.showAt(e.getXY());
        });

        var rMenu1 = new Ext.menu.Item({
          text:'无限制',
          handler: function(){
            changeType(0)
          }
        })

        var rMenu2 = new Ext.menu.Item({
          text:'有静有动',
          handler: function(){
            changeType(1)
          }
        })

        var rMenu3 = new Ext.menu.Item({
          text:'有静无动',
          handler: function(){
            changeType(2)
          }
        })
        var rightClick = new Ext.menu.Menu({
          id:'rightClickCont',
          items: [
            rMenu1,rMenu2,rMenu3
          ]
        });
        standardMaintenanceGrid.render('standardMaintenanceList');
        standardMaintenanceDS.load();
      })
    </script>
  </head>
  <body>
    <div id="standardMaintenanceList"></div>
  </body>
</html>
